package com.mengsenlin.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/7/24.
 */

public class LoginInterceptor implements HandlerInterceptor {
    private final String LOGIN_ACTION = "/login.action";
    private final String REGISTER_ACTION = "/register.action";
    private final String SIGNIN_ACTION = "/signin.action";
    private final String SIGNUP_ACTION = "/signup.action";

    private boolean check1(String url){
        if(!LOGIN_ACTION.equals(url) && !REGISTER_ACTION.equals(url) && !SIGNIN_ACTION.equals(url) && !SIGNUP_ACTION.equals(url)){
            return true;
        }
        return false;
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //return false表示拦截，不向下执行
        //return true表示放行
//        System.out.println(request.getServletPath());
//        String url = request.getServletPath();
//        HttpSession session = request.getSession();
//        if(null != session && null != session.getAttribute("user")){
//            if(LOGIN_ACTION.equals(url) || REGISTER_ACTION.equals(url)){
//                response.sendRedirect("/home.action");
//                return false;
//            }
//        }else{
//            if(check1(url)){
//                response.sendRedirect("/login.action");
//                return false;
//            }
//        }
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
